package toolx

import "fmt"

// WkCfg 配置文件
type WkCfg struct {
	GoModule string   `mapstructure:"go-module"`
	DB       DBConfig `mapstructure:"db"`
}

// DBConfig 数据库配置
type DBConfig struct {
	Driver  string `mapstructure:"driver"`
	Dialect string `mapstructure:"dialect"`
	Host    string `mapstructure:"host"`
	Port    int    `mapstructure:"port"`
	User    string `mapstructure:"user"`
	Pass    string `mapstructure:"pass"`
	Name    string `mapstructure:"name"`
}

// DSN 生成数据库连接字符串
func (db DBConfig) DSN() string {
	switch db.Dialect {
	case "mysql":
		return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=true&loc=Local", db.User, db.Pass, db.Host, db.Port, db.Name)
	case "postgresql":
		return fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", db.Host, db.Port, db.User, db.Pass, db.Name)
	case "sqlite":
		return db.Name
	}

	panic("no adapter")
}
